package interceptor;

import java.sql.Connection;

import util.DBUtil;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class Transaction extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Connection con=DBUtil.openInThread();
		con.setAutoCommit(false);//后续的数据库处理时不关闭连接
		try {
			String resultCode=invocation.invoke();
			con.commit();
			return resultCode;
		} catch (Exception e) {
			e.printStackTrace();
			con.rollback();
			return "error";
		}	finally{
			DBUtil.closeInThread();
		}
	}
	
}
